package com.uinnova.product.eam.web.bm.mvc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.binary.framework.util.ControllerUtils;
import com.binary.framework.web.RemoteResult;
import com.uinnova.product.eam.model.dto.VisualModelsDto;
import com.uinnova.product.eam.model.vo.VisualModelsVo;
import com.uinnova.product.eam.web.bm.peer.VisualModelsPeer;
import com.uinnova.product.vmdb.comm.doc.annotation.ModDesc;
import com.uinnova.product.vmdb.provider.ci.bean.CcCiClassInfo;
import com.uino.util.sys.SysUtil;
import com.uino.bean.cmdb.base.ESVisualModel;
import com.uino.bean.permission.base.SysUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description: 元模型
 * @author: Lc
 * @create: 2021-11-04 10:01
 */
@RestController
@RequestMapping("/bm/visual/model")
public class VisualModelsMvc {

    @Resource
    private VisualModelsPeer visualModelsPeer;

    @PostMapping("/handlerVisualModels")
    public RemoteResult handlerVisualModels(@RequestBody String body) {
        JSONObject json = JSON.parseObject(body);
        Long modelId = json.getLong("id");
        // 状态，0：取消编辑  1：编辑
        Integer status = json.getInteger("status");
        Map<String, Object> result = visualModelsPeer.handlerVisualModels(modelId, status);
        return new RemoteResult(result);
    }


    @PostMapping("queryVisualModels")
    public void queryVisualModels(@RequestBody String body,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        List<Long> sheetIds = (List)JSONArray.parseArray(body);
        SysUser user = SysUtil.getCurrentUserInfo();
        List<VisualModelsVo> models = visualModelsPeer.queryVisualModels(sheetIds, user.getDomainId());
        ControllerUtils.returnJson(request, response, models);
    }

    @GetMapping("/exportVisualModels")
    public RemoteResult exportVisualModels() {
        boolean result = visualModelsPeer.exportVisualModels();
        return new RemoteResult(result);
    }

    @PostMapping("/importVisualModels")
    public RemoteResult importVisualModels(@RequestBody VisualModelsDto dto) {
        boolean result = visualModelsPeer.importVisualModels(dto);
        return new RemoteResult(result);
    }

    /**
     * 保存可视化模型
     * @param model
     * @return
     */
    @PostMapping("/saveVisualModel")
    @ModDesc(desc = "保存可视化模型", pDesc = "可视化模型数据传输对象", pType = ESVisualModel.class, rDesc = "可视化模型id", rType = Long.class)
    public RemoteResult saveVisualModel(@RequestBody ESVisualModel model) {
        Long status = visualModelsPeer.saveVisualModel(model);
        return new RemoteResult(status);
    }

    /**
     * 删除元模型下的sheet页缩略图
     * @param dto
     * @return
     */
    @PostMapping("/delVMThumbnailBySheetId")
    public RemoteResult delVMThumbnailBySheetId(@RequestBody VisualModelsDto dto) {
        visualModelsPeer.delVMThumbnailBySheetId(dto.getVisualModelId(), dto.getSheetId());
        return new RemoteResult(true);
    }

    @PostMapping(value = "/refreshClassIdByVisId")
    @ModDesc(desc = "根据元模型ID刷新json数据", pDesc = "对象分类数据", pType = CcCiClassInfo.class, rDesc = "分类ID", rType = Long.class)
    public void refreshClassIdByVisId(HttpServletRequest request, HttpServletResponse response, @RequestBody Long visId) {
        Map<Long, Long> res = visualModelsPeer.refreshClassIdByVisId(visId);
        ControllerUtils.returnJson(request, response, res);
    }

    /**
     * 获取当前用户操作状态
     * @return
     */
    @PostMapping("/isEmploy")
    public RemoteResult isEmploy(@RequestBody VisualModelsDto dto) {
        Boolean status = visualModelsPeer.isEmploy(dto.getVisualModelId());
        return new RemoteResult(status);
    }

}
